import { AddMobileContactData } from '@/utils/common-api.js'
export default {
  data() {
    return {
      productTime: '',
      contractid: '',
      jujueShow: false,
      isshow: false,
      texts: '',
      pageDetail: {},
      expressListAction: [
        {
          val: 0,
          name: '合同发起时间',
        },
        {
          val: 1,
          name: '定金支付时间',
        },
        {
          val: 2,
          name: '确认合同时间',
        },
        {
          val: 3,
          name: '发货时间',
        },
        {
          val: 4,
          name: '确认收货时间',
        },
      ],
      statusList: [
        {
          val: 0,
          name: '待付款',
        },
        {
          val: 1,
          name: '待确认',
        },
        {
          val: 2,
          name: '待发货',
        },
        {
          val: 3,
          name: '待收货',
        },
        {
          val: 4,
          name: '已完成',
        },
        {
          val: 5,
          name: '取消',
        },
      ], //合同状态 0、待付款 1、待确认 2、待发货 3、待收货 4、完成
      ordertab: '', //contract_type 0买车  1卖车
      bframe: false,
      expressList: [], //物流公司列表
      expressShow: false, //物流公司弹框状态
      expressName: '', //物流公司名称
      expressVal: '', //快递单号
      getGoodshow: false, //是否确认收货弹框
      cardetailList: [], //车辆详情json
      carDateList: [
        {
          //交车时间list
          value: 0,
          label: '7日内交车',
        },
        {
          value: 1,
          label: '15日内交车',
        },
        {
          value: 2,
          label: '30日内交车',
        },
      ],
      certificateList: [
        {
          //车辆合格证list
          label: '随车',
          value: 0,
        },
        {
          label: '邮寄',
          value: 1,
        },
      ],
      popup: {
        //页面弹框控制
        show: false,
        title: '',
        btnText: '',
        type: '',
      },
      pageFrom: '', //从哪里进入详情
    }
  },
  onLoad(option) {
    console.log(option, this.$store.state.userInfoDetail.store.storeId)
    this.contractid = option.id //合同id
    this.ordertab = option.tab //列表类型
    this.pageFrom = option.pageFrom ? option.pageFrom : ''
    this.getPageData()
    // if(this.pageDetail.contract_status=='2'){
    this.getexpresscompany()
    this.getCarDetailJson()
    // }
  },
  created() {},
  methods: {
    // 获取物流公司
    async getexpresscompany() {
      try {
        const { data } = await this.$api.jsonData.getexpresscompany()
        console.log(data)
        this.expressList = data.expressArr.concat(data.logisticsArr)
        this.expressList.forEach((it) => {
          it.value = it.name
          it.label = it.name
        })
        console.log(this.expressList)
      } catch (e) {
        //TODO handle the exception
      }
    },
    // 选择物流公司
    selectExpress(e) {
      console.log(e)
      this.expressName = e[0].value
    },
    // 车辆详情json
    getCarDetailJson() {
      this.$api.jsonData.getCarDetail().then((res) => {
        console.log('车辆详情json', res)
        this.cardetailList = res.data.list
      })
    },
    // 收货 删除 取消 弹出提示框
    setPopup(type) {
      let title = '',
        btn = ''
      if (type === '取消') {
        title = '请您确认是否要取消该订单'
        btn = '取消订单'
      } else if (type === '删除') {
        title = '请您确认是否要删除该订单'
        btn = '删除订单'
      } else {
        title = '请您确认是否已经收到完好货品'
        btn = '确认收货'
      }
      this.popup = {
        show: true,
        title: title,
        btnText: btn,
        type: type,
      }
    },
    // 获取详情
    async getPageData() {
      uni.showLoading({
        title: '加载中',
      })
      var req = {
        id: this.contractid,
        type: this.type,
      }
      const res = await this.$api.carOrder.ContractDetailsDataListTask(req)
      try {
        uni.hideLoading()
        res.data.list[0].car_colour = JSON.parse(res.data.list[0].car_colour)
        this.pageDetail = res.data.list[0]
        this.pageDetail.produced_date = this.pageDetail.produced_date.split(
          ' '
        )[0]
        console.log(this.pageDetail)
      } catch (e) {
        //TODO handle the exception
      }
    },
    // 订单状态
    getStatus(val) {
      let name = ''
      this.statusList.forEach((it) => {
        if (it.val == val) {
          name = it.name
        }
      })
      return name
    },
    // 去付款
    goPay() {
      console.log(this.pageDetail)
      uni.navigateTo({
        url:
          '/pages_order/payDeposit/payDeposit?id=' +
          this.contractid +
          '&deposit_amount=' +
          this.pageDetail.deposit_amount +
          '&time=' +
          this.pageDetail.endtime,
      })
    },
    // 录入物流信息
    async confirmDelivery() {
      let data = {
        logisticsname: this.expressName,
        logisticsnumber: this.expressVal,
        contractid: this.contractid,
        storeid: this.$store.state.userInfoDetail.store.storeId,
      }
      console.log(111, data)
      for (let it in data) {
        if (data[it] === '') {
          uni.showToast({
            title: '请完善物流信息',
            duration: 2000,
            icon: 'none',
          })
          return false
        }
      }
      const res = await this.$api.carOrder.DepartOrderTask(data)
      try {
        console.log('录入物流信息', res)
        if (res.code === 200) {
          uni.showToast({
            title: '录入成功',
            duration: 2000,
            icon: 'success',
          })
          setTimeout(function () {
            uni.redirectTo({
              url: '/pages_order/orderAdmin/orderAdmin',
            })
          }, 2000)
        }
      } catch (e) {
        //TODO handle the exception
      }
    },
    // 确认合同
    async viewContract() {
      let data = {
        contractid: this.contractid,
        storeid: this.$store.state.userInfoDetail.store.storeId,
      }
      const res = await this.$api.carOrder.AffirmOrderTask(data)
      try {
        console.log('确认合同', res)
        if (res.code === 200) {
          uni.showToast({
            title: '已确认',
            duration: 2000,
            icon: 'success',
          })
          setTimeout(function () {
            uni.redirectTo({
              url: '/pages_order/orderAdmin/orderAdmin',
            })
          }, 2000)
        }
      } catch (e) {
        //TODO handle the exception
      }
    },
    // 确认收货
    async confirmReceipt() {
      var req = {
        contractid: this.contractid,
        storeid: this.$store.state.userInfoDetail.store.storeId,
      }
      const res = await this.$api.carOrder.OrderReceiveTask(req)
      try {
        console.log('确认收货', res)
        if (res.code === 200) {
          this.popup.show = false
          // CancelOrderTask
          uni.showToast({
            title: '已确认',
            duration: 2000,
            icon: 'success',
          })
          setTimeout(function () {
            uni.redirectTo({
              url: '/pages_order/orderAdmin/orderAdmin',
            })
          }, 2000)
        }
      } catch (e) {
        //TODO handle the exception
      }
    },
    // 删除订单
    async DeleteOrderTask() {
      var req = {
        contractid: this.contractid,
        storeid: this.$store.state.userInfoDetail.store.storeId,
      }
      const res = await this.$api.carOrder.DeleteOrderTask(req)
      try {
        console.log('删除', res)
        if (res.code === 200) {
          this.popup.show = false
          uni.showToast({
            title: '删除成功',
            duration: 2000,
            icon: 'success',
          })
          setTimeout(function () {
            uni.redirectTo({
              url: '/pages_order/orderAdmin/orderAdmin',
            })
          }, 2000)
        }
      } catch (e) {
        //TODO handle the exception
      }
    },
    // 取消订单
    async CancelOrderTask() {
      var req = {
        contractid: this.contractid,
        storeid: this.$store.state.userInfoDetail.store.storeId,
      }
      const res = await this.$api.carOrder.CancelOrderTask(req)
      try {
        console.log('取消', res)
        if (res.code === 200) {
          this.popup.show = false
          uni.showToast({
            title: '取消成功',
            duration: 2000,
            icon: 'success',
          })
          setTimeout(function () {
            uni.redirectTo({
              url: '/pages_order/orderAdmin/orderAdmin',
            })
          }, 2000)
        }
      } catch (e) {
        //TODO handle the exception
      }
    },
    // 返回物流公司
    returnExpressAction(val) {
      let name = ''
      this.expressListAction.forEach((it) => {
        if (val == it.val) {
          name = it.name
        }
      })
      return name
    },
    // 返回车辆详情
    returnCarDetail(item) {
      let name = ''
      this.cardetailList.forEach((it) => {
        if (item == it.value) {
          name = it.label
        }
      })
      return name
    },
    // 返回交车日期
    returncarDate(item) {
      let name = ''
      this.carDateList.forEach((it) => {
        if (item == it.value) {
          name = it.label
        }
      })
      return name
    },
    // 返回交车日期
    returncarDate(item) {
      let name = ''
      this.certificateList.forEach((it) => {
        if (item == it.value) {
          name = it.label
        }
      })
      return name
    },
    // 复制
    copy(data) {
      uni.setClipboardData({
        data: data,
        success: function () {
          console.log('success')
          uni.showToast({ title: '复制成功' })
        },
      })
    },

    // 拨打电话
    call(type) {
      // console.log(this.pageDetail)
      AddMobileContactData(this.$store.state.userInfoDetail.store.storeId)
      uni.makePhoneCall({
        phoneNumber: type === 1 ? '4000909987' : this.pageDetail.sellermobile, //仅为示例
      })
    },
    //联系商家/买家
    contactSeller() {
      //0买车  1卖车
      if (this.pageFrom === 'chat') {
        uni.navigateBack()
        return
      }
      this.$store.commit('news/setCardInfo', {
        imgSrc: this.pageDetail.goodimg,
        name: this.pageDetail.car_name,
        price: this.pageDetail.payable_amount,
        type: 'orderInfo',
      })

      if (this.ordertab === '0') {
        //与商家聊天
        this.$store.commit('news/createConversationActive', {
          user: this.pageDetail.sellerstorename, //名称
          img: '', //头像
          lastTime: '', //
          userId: this.pageDetail.sellerstoreid, //id
          conversationID: 'C2C' + this.pageDetail.sellerstoreid,
        })

        uni.navigateTo({
          url: `/pages_news/contactSeller/contactSeller?user=${this.pageDetail.sellerstorename}&pageId=${this.pageDetail.id}&tab=${this.ordertab}`,
        })
      } else {
        //与买家聊天
        this.$store.commit('news/createConversationActive', {
          user: this.pageDetail.buyerstorename, //名称
          img: '', //头像
          lastTime: '', //
          userId: this.pageDetail.buyerstoreid, //id
          conversationID: 'C2C' + this.pageDetail.buyerstoreid,
        })
        uni.navigateTo({
          url: `/pages_news/contactSeller/contactSeller?user=${this.pageDetail.buyerstorename}&pageId=${this.pageDetail.id}&tab=${this.ordertab}`,
        })
      }
    },
  },
}
